﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Supermarket;
using AForge_zxing;

namespace XLSupermarket.Storehouse.Commodity_warehousing
{
    public partial class Commodity_warehousing : Form
    {
        public Commodity_warehousing()
        {
            InitializeComponent();
        }

        private void Commodity_warehousing_Load(object sender, EventArgs e)
        {
            try { 
            GHS();
            LX();
            //清空
            this.comboBox1.SelectedIndex = 0;
            this.textBox2.Text = "";
            this.textBox4.Text = "";
            this.comboBox2.SelectedIndex = 0;
            this.textBox10.Text = "";
            this.dateTimePicker1.Text = DateTime.Now.ToString();
            this.textBox6.Text = "";
            this.textBox3.Text = "";
            this.textBox7.Text = "";
            this.dateTimePicker2.Text = DateTime.Now.ToString();
            this.textBox9.Text = "";
            }
            catch (Exception ex)
            {
                MessageBox.Show("出现错误：" + ex.Message);
            }
        }

        private void GHS() {
            try { 
            string sql = "select * from Goods_Type";
            comboBox1.DataSource = DBHelper.FillData(sql);
            comboBox1.DisplayMember = "GoodsType_Name";
            comboBox1.ValueMember = "GoodsType_ID";
            }
            catch (Exception ex)
            {
                MessageBox.Show("出现错误：" + ex.Message);
            }
        }

        private void LX() {
            try { 
            string sql = "select * from Supplier";
            comboBox2.DataSource = DBHelper.FillData(sql);
            comboBox2.DisplayMember = "Supplier_Name";
            comboBox2.ValueMember = "Supplier_ID";
            }
            catch (Exception ex)
            {
                MessageBox.Show("出现错误：" + ex.Message);
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            try { 
            //供货商编号
            string Supplier_ID = this.comboBox2.SelectedValue.ToString();
            //条形码
            string Bar_code = this.textBox2.Text;
            //商品名称
            string Trade_Name = this.textBox4.Text;
            //商品类型编号
            string Supplier_CommoditytypeID = this.comboBox1.SelectedValue.ToString();
            //商品进价
            string Purchase_price = this.textBox10.Text;
            //入库时间
            string datetime = this.dateTimePicker1.Value.ToString();
            //入库数量
            string Number = this.textBox6.Text;
            //规格
            string Specifications = this.textBox3.Text;
            //备注
            string Other = this.textBox7.Text;
            //生产日期
            string DateOfManufacture = this.dateTimePicker2.Value.ToString();
            //保质期
            string QualityGuaranteePeriod = this.textBox9.Text;
            //零售价
            string Ratail_Price = TB_LS.Text;
            //会员价
            string Membership_Price = TB_VIPSJ.Text;
            //折扣
            string Discount = TB_ZK.Text;
            //判断数据输入
            if (Supplier_ID==""|| Bar_code==""|| Trade_Name==""|| Supplier_CommoditytypeID==""|| Purchase_price==""||
               datetime==""|| Number==""|| double.Parse(Number)<=0|| Specifications==""||DateOfManufacture==""||
               QualityGuaranteePeriod==""|| double.Parse(QualityGuaranteePeriod)<=0|| Ratail_Price==""|| double.Parse(Ratail_Price)<=0||
               Membership_Price==""|| double.Parse(Membership_Price)<=0|| Discount==""||double.Parse(Discount)<=0||double.Parse(Discount)>1) {
                MessageBox.Show("请检查数据输入！");
                return;
            }
            //商品入库
            string sql = string.Format("insert into Goods_Warehousing values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}')",
                Supplier_ID, Bar_code, Trade_Name, Supplier_CommoditytypeID,  Purchase_price, datetime, Number, Specifications, Other, QualityGuaranteePeriod, DateOfManufacture);
                MessageBox.Show(sql);
            int result = DBHelper.Edit(sql);
            sql = "select * from Merchandise_inventory where Bar_code='"+ Bar_code + "'";
            if (DBHelper.Edit(sql) > 0)
            {
                //存在条码
                sql = string.Format("update Merchandise_inventory set Inventory_quantity+='{0}' where Bar_code='{1}'",
                    Number, Bar_code);
            }
            else {
                //不存在条码
                sql = string.Format("insert into Merchandise_inventory Values ('{0}','{1}','{2}','{3}','{4}','{5}','{6}')",
                Supplier_ID, Bar_code, Ratail_Price, Membership_Price, Number, Discount, Other);
            }
            result = DBHelper.Edit(sql); 
            //判断是否新增成功
            if (result>0) MessageBox.Show("商品入库成功！");
            else MessageBox.Show("商品入库失败!");
            }
            catch (Exception ex)
            {
                MessageBox.Show("出现错误：" + ex.Message);
            }
        }
        /// <summary>
        /// 返回按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
           
            this.Hide();
        }
        /// <summary>
        /// 清空
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            //清空
            this.comboBox1.SelectedIndex = 0;
            this.textBox2.Text = "";
            this.textBox4.Text = "";
            this.comboBox2.SelectedIndex = 0;
            this.textBox10.Text = "";
            this.dateTimePicker1.Text = DateTime.Now.ToString();
            this.textBox6.Text = "";
            this.textBox3.Text = "";
            this.textBox7.Text = "";
            this.dateTimePicker2.Text = DateTime.Now.ToString();
            this.textBox9.Text = "";
            TB_LS.Text = "";
            TB_VIPSJ.Text = "";
            TB_ZK.Text = "";
        }
        /// <summary>
        /// 扫码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button6_Click(object sender, EventArgs e)
        {
            try { 
            AForge_zxing.Form1 AF = new AForge_zxing.Form1();
            DialogResult result = AF.ShowDialog();
            if (result == DialogResult.OK)
            {
                textBox2.Text = AF.reisbn;
            }
            }
            catch (Exception ex)
            {
                MessageBox.Show("出现错误：" + ex.Message);
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            Commodity_warehousing_table CWT = new Commodity_warehousing_table();
            CWT.ShowDialog();
        }
    }
}
